package com.spring.sunflower.main.adapter

import android.content.Context
import android.graphics.Color
import android.graphics.ColorFilter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.ImageView
import android.widget.TextView
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.makeramen.roundedimageview.RoundedImageView
import com.spring.sunflower.R
import com.spring.sunflower.bean.VideoCommentBean

/**
 * Created by Kevin on 2021/11/10<br/>
 *
 * Describe:<br/>
 */
class VideoCommentListAdapter(
    var context: Context,
    var data: MutableList<VideoCommentBean.DatasBean>
) : BaseExpandableListAdapter() {
    fun addComment(data: MutableList<VideoCommentBean.DatasBean>) {
        data.addAll(data)
        notifyDataSetChanged()
    }

    override fun getGroupCount(): Int = data.size

    override fun getChildrenCount(groupPosition: Int): Int =
        data[groupPosition].subCommentList?.size ?: 0

    override fun getGroup(groupPosition: Int): Any {
        return data[groupPosition]
    }

    override fun getChild(groupPosition: Int, childPosition: Int): Any {
        return data[groupPosition].subCommentList[childPosition]
    }

    override fun getGroupId(groupPosition: Int): Long = groupPosition.toLong()

    override fun getChildId(groupPosition: Int, childPosition: Int): Long = childPosition.toLong()

    override fun hasStableIds(): Boolean = true

    override fun getGroupView(
        groupPosition: Int,
        isExpanded: Boolean,
        convertView: View?,
        parent: ViewGroup?
    ): View {
        var tempView = convertView
        var viewHolder: GroupViewHolder? = null
        if (tempView == null) {
            tempView = LayoutInflater.from(context)
                .inflate(R.layout.layout_video_show_comment_parent, null)
            viewHolder = GroupViewHolder()
            viewHolder.avatar = tempView.findViewById<RoundedImageView>(R.id.rivAvatar)
            viewHolder.nickname = tempView.findViewById(R.id.tvNickname)
            viewHolder.comment = tempView.findViewById(R.id.tvParentComment)
            viewHolder.favorite = tempView.findViewById(R.id.ivCommentFav)
            tempView.tag = viewHolder
        } else {
            viewHolder = tempView.tag as GroupViewHolder
        }
        with(data[groupPosition]) {
            Glide.with(context)
                .load(photo)
                .placeholder(R.drawable.ic_placeholder)
                .into(viewHolder.avatar!!)
            viewHolder.nickname!!.text = nickname
            viewHolder.comment!!.text = content
            viewHolder.favorite!!.setColorFilter(if (isLike=="1")Color.parseColor("#E57373") else Color.parseColor("#cccccc"))
        }
        viewHolder.favorite!!.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
//                ToastUtils.showShort("点赞")
                if (data[groupPosition].isLike == "1") return
                listener?.onParentFav(groupPosition)
            }

        })
        return tempView!!

    }

    override fun getChildView(
        groupPosition: Int,
        childPosition: Int,
        isLastChild: Boolean,
        convertView: View?,
        parent: ViewGroup?
    ): View {
        var tempView = convertView
        var viewHolder: ChildViewHolder
        if (tempView == null) {
            tempView = LayoutInflater.from(context)
                .inflate(R.layout.layout_video_show_comment_child, null)
            viewHolder = ChildViewHolder()
            viewHolder.avatar = tempView.findViewById<RoundedImageView>(R.id.rivAvatar)
            viewHolder.nickname = tempView.findViewById(R.id.tvNickname)
            viewHolder.comment = tempView.findViewById(R.id.tvChildComment)
            viewHolder.favorite = tempView.findViewById(R.id.ivCommentFav)
            tempView.tag = viewHolder
        } else {
            viewHolder = tempView.tag as ChildViewHolder
        }
        with(data[groupPosition].subCommentList[childPosition]) {
            Glide.with(context)
                .load(photo)
                .placeholder(R.drawable.ic_placeholder)
                .into(viewHolder.avatar!!)
            viewHolder.nickname!!.text = nickname
            viewHolder.comment!!.text = content
            viewHolder.favorite!!.setColorFilter(if (isLike=="1")Color.parseColor("#E57373") else Color.parseColor("#cccccc"))
        }
        viewHolder.favorite!!.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
//                ToastUtils.showShort("点赞")
                if (data[groupPosition].subCommentList[childPosition].isLike == "1") return
                listener?.onChildFav(groupPosition, childPosition)

            }

        })
        return tempView!!
    }

    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean = true

    class GroupViewHolder {
        var avatar: RoundedImageView? = null
        var nickname: TextView? = null
        var comment: TextView? = null
        var favorite: ImageView? = null
    }

    class ChildViewHolder {
        var avatar: RoundedImageView? = null
        var nickname: TextView? = null
        var comment: TextView? = null
        var favorite: ImageView? = null
    }

    var listener: OnFavClickListener? = null
    fun setOnFavClickListener(l: OnFavClickListener) {
        listener = l
    }

    interface OnFavClickListener {
        fun onParentFav(groupPosition: Int)
        fun onChildFav(groupPosition: Int, childPosition: Int)
    }
}